import type { ArtColumn } from 'ali-react-table';
import { Button, Tooltip } from 'antd';
import moment from 'moment';

const fileTypeOptions = [
  {
    label: '交易执行',
    value: 1,
  },
  {
    label: '解决方案',
    value: 2,
  },
];

const columns = ({
  preview,
  download,
  del,
}: {
  preview: (row: any) => void;
  download: (row: any) => void;
  del: (row: any) => void;
}): ArtColumn[] => {
  return [
    { code: 'marketName', name: '省份', width: 140, align: 'center', lock: true },
    {
      code: 'fileTypeNmae',
      name: '文件类别',
      width: 80,
      align: 'center',
      lock: true,
      render: (value) => {
        return (
          <Tooltip placement="top" title={value}>
            <div className="ellipsis">{value}</div>
          </Tooltip>
        );
      },
    },
    {
      code: 'fileNameSuffixed',
      name: '文件名称',
      width: 160,
      align: 'center',
      render: (value) => {
        return (
          <Tooltip placement="top" title={value}>
            <div className="ellipsis">{value}</div>
          </Tooltip>
        );
      },
    },
    { code: 'createdBy', name: '所有者', width: 100, align: 'center' },
    {
      code: 'createdAt',
      name: '创建时间',
      width: 100,
      align: 'center',
      render: (value) => moment(value).format('YYYY-MM-DD'),
    },
    { code: 'fileSize', name: '文件大小', width: 80, align: 'center' },
    {
      code: 'options',
      name: '操作',
      width: 150,
      align: 'center',
      render: (value, row) => (
        <>
          <Button type="link" onClick={() => download(row)}>
            下载
          </Button>
          <Button type="link" onClick={() => preview(row)}>
            预览
          </Button>
          <Button type="link" onClick={() => del(row)}>
            删除
          </Button>
        </>
      ),
    },
  ];
};

const downFile = (fileUrl: string, fileName: string = '未命名') => {
  if (!fileUrl) return;
  const a = document.createElement('a');
  a.href = fileUrl;
  a.download = fileName;
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
  window.URL.revokeObjectURL(fileUrl);
};

const audioExtensions = ['mp3', 'wav', 'flac', 'aac', 'ogg', 'm4a', 'wma', 'aiff', 'amr', 'opus'];
const videoExtensions = [
  'mp4',
  'mkv',
  'avi',
  'mov',
  'wmv',
  'flv',
  'webm',
  'm4v',
  'ts',
  '3gp',
  'rmvb',
];

const onlinePre = ['ppt', 'pptx', 'doc', 'docx', 'xls', 'xlsx'];

const imgs = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'];

const getFileType = (fileName: string): 'notSupport' | 'online' | 'download' | 'img' => {
  const extension = fileName.split('.').pop()?.toLowerCase(); // 获取文件后缀名
  if (audioExtensions.includes(extension || '') || videoExtensions.includes(extension || '')) {
    return 'notSupport';
  }

  if (onlinePre.includes(extension || '')) {
    return 'online';
  }

  if (imgs.includes(extension || '')) {
    return 'img';
  }

  if (extension === 'pdf') {
    return 'download';
  }

  return 'notSupport';
};

export { fileTypeOptions, columns, downFile, getFileType };
